package com.hugo.komovie_first.utils;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hugo.komovie_first.R;

import java.io.File;

public class ImageLoader {
    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";
    private static class ImageLoaderHolder {
        private static final ImageLoader INSTANCE = new ImageLoader();
    }

    private ImageLoader() {
    }

    public static final ImageLoader getInstance() {
        return ImageLoaderHolder.INSTANCE;
    }


    //ֱ�Ӽ�������ͼƬ
    public void displayImage(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .centerCrop()
                .crossFade()
                .into(imageView);
    }


    //����SD��ͼƬ
    public void displayImage(Context context, File file, ImageView imageView) {
        Glide
                .with(context)
                .load(file)
                .centerCrop()
                .into(imageView);

    }

    //����SD��ͼƬ�����ô�С
    public void displayImage(Context context, File file, ImageView imageView, int width, int height) {
        Glide
                .with(context)
                .load(file)
                .override(width, height)
                .centerCrop()
                .into(imageView);

    }

    //��������ͼƬ�����ô�С
    public void displayImage(Context context, String url, ImageView imageView, int width, int height) {
        Glide
                .with(context)
                .load(url)
                .centerCrop()
                .override(width, height)
                .crossFade()
                .into(imageView);
    }

    //����drawableͼƬ
    public void displayImage(Context context, int resId, ImageView imageView) {
        Glide.with(context)
                .load(resourceIdToUri(context, resId))
                .crossFade()
                .into(imageView);
    }

    //����drawableͼƬ��ʾΪԲ��ͼƬ
    public void displayCricleImage(Context context, int resId, ImageView imageView) {
        Glide.with(context)
                .load(resourceIdToUri(context, resId))
                .crossFade()
                .transform(new GlideCircleTransform(context))
                .into(imageView);
    }

    //��������ͼƬ��ʾΪԲ��ͼƬ
    public void displayCricleImage(Context context, String url, ImageView imageView) {
        Glide
                .with(context)
                .load(url)
                        //.centerCrop()//���ѷ��������ô����Կ��ܲ�������,����Щ�豸�Ͽ��ܻ᲻����ʾΪԲ�Ρ�
                .transform(new GlideCircleTransform(context))
                .crossFade()
                .into(imageView);
    }

    //����SD��ͼƬ��ʾΪԲ��ͼƬ
    public void displayCricleImage(Context context, File file, ImageView imageView) {
        Glide .with(context)
                .load(file)
                        //.centerCrop()
                .transform(new GlideCircleTransform(context))
                .into(imageView);

    }

    //����ԴIDתΪUri
    public Uri resourceIdToUri(Context context, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
    }

}